package com.liuzh.fitnessplan.activity

import android.app.AlarmManager
import android.app.Notification
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.drawable.Drawable
import android.os.Build
import android.os.Bundle
import android.util.Log
import android.widget.TextView
import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentTransaction
import com.liuzh.fitnessplan.R
import com.liuzh.fitnessplan.base.BaseActivity
import com.liuzh.fitnessplan.broadcast.AlarmReceiver
import com.liuzh.fitnessplan.databinding.ActivityMainBinding
import com.liuzh.fitnessplan.fragment.MainFragment
import com.liuzh.fitnessplan.fragment.ManagerFragment
import com.liuzh.fitnessplan.fragment.PersonalFragment
import java.text.SimpleDateFormat
import java.util.*


class MainActivity : BaseActivity() {
    private var barValue : Int = 0
    private val alarmReceiver = AlarmReceiver()
    private lateinit var bind : ActivityMainBinding
    private val fragmentManager = supportFragmentManager
    private val list = mutableListOf<Fragment>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bind = ActivityMainBinding.inflate(layoutInflater)
        setContentView(bind.root)
        initData()
        setClickListener()
        setSelect(bind.barMain,R.drawable.main_black,2)

    }

    private fun initData() {

        val mainFragment = MainFragment()
        val managerFragment = ManagerFragment()
        val personalFragment = PersonalFragment()

        list.add(managerFragment)
        list.add(mainFragment)
        list.add(personalFragment)
    }

    private fun setClickListener() {
        bind.barMain.setOnClickListener {
            Log.d("2","2")
            setSelect(bind.barMain,R.drawable.main_black,2)
        }
        bind.barManager.setOnClickListener {
            Log.d("1","1")
            setSelect(bind.barManager,R.drawable.manager_black,1)
        }
        bind.barPersonal.setOnClickListener {
            Log.d("3","3")
            setSelect(bind.barPersonal,R.drawable.personal_black,3)
        }
    }

    private fun setFragment(fragment : Fragment){
        val fragmentTransaction : FragmentTransaction = fragmentManager.beginTransaction()
        for(fg in fragmentManager.fragments){
            fragmentTransaction.hide(fg)
        }
        if(fragment.isAdded){
            fragmentTransaction.show(fragment)
        }else{
            fragmentTransaction.add(bind.fragment.id,fragment)
        }
        fragmentTransaction.commit()
    }

    private fun setSelect(textView : TextView , id : Int, value : Int){
        if(barValue == value){
            return
        }
        setFragment(list[value-1])
        val black = ContextCompat.getColor(this,R.color.bar_black)
        val gray = ContextCompat.getColor(this,R.color.bar_gray)
        bind.barMain.setTextColor(gray)
        bind.barPersonal.setTextColor(gray)
        bind.barManager.setTextColor(gray)

        bind.barMain.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableResource(R.drawable.main_gray),null,null)
        bind.barPersonal.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableResource(R.drawable.personal_gray),null,null)
        bind.barManager.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableResource(R.drawable.manager_gray),null,null)

        textView.setTextColor(black)
        textView.setCompoundDrawablesWithIntrinsicBounds(null,getDrawableResource(id),null,null)

        barValue = value
    }

    private fun getDrawableResource(id:Int):Drawable?{
        return ContextCompat.getDrawable(this,id)
    }


    override fun onStart() {
        super.onStart()
        val intentFilter = IntentFilter()
        intentFilter.addAction("com.liuzh.ALARM_BROADCAST")
        registerReceiver(alarmReceiver,intentFilter)

    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(alarmReceiver)
    }
}
